//
//  MDDStoreViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/5/28.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MDDStoreViewController.h"
#import "MoreDropDownMenu.h"
#import "ShopKindItem.h"
#import "AppBaseGoodItem.h"
#import "HomeGoodItemCCell.h"
#import "MDDGoodsDetailViewController.h"
#import "AddCartOrBuyView.h"
#import "MDDGoodsDetailViewController.h"
#import "LLSearchViewController.h"

@interface MDDStoreViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,MoreDropDownMenuDataSource,MoreDropDownMenuDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *selfCollectionView;
@property (nonatomic, strong) MoreDropDownMenu       *menu;
@property (nonatomic,strong) NSMutableArray *goodKindItems;
@property (nonatomic,strong) NSMutableArray *allGoodItems;
@property (nonatomic,assign) NSInteger page;

@property (nonatomic,strong) ShopKindItem *selKindItem;
@property (nonatomic,strong) NSDictionary *selTwoKinds;
@end

@implementation MDDStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selfCollectionView.backgroundColor = [UIColor AxcUI_colorWithHexCode:@"#ebebeb"];
    self.selfCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadAllGoods];
    }];
    self.selfCollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadAllGoods];
    }];
    self.goodKindItems = [[NSMutableArray alloc]init];
    self.allGoodItems = [[NSMutableArray alloc]init];
    [self loadGoodKinds];
    [self loadAllGoods];
    
    UIView *navMiddleV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mScreenWidth-140, 32)];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(searchAction)];
    [navMiddleV addGestureRecognizer:tap];
    navMiddleV.backgroundColor = [UIColor clearColor];
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, mScreenWidth-140, 32)];
    imageView.image = mImageByName(@"首页搜索框");
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [navMiddleV addSubview:imageView];
    self.navigationItem.titleView = navMiddleV;
}

-(void)searchAction
{
    LLSearchViewController *seachVC = [[LLSearchViewController alloc] init];
    seachVC.hidesBottomBarWhenPushed = YES;
    seachVC.searchType = 3;
    [self.navigationController pushViewController:seachVC animated:YES];
//    MDDSearchPlantGoodsVC *seachVC = [[MDDSearchPlantGoodsVC alloc] init];
//    seachVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:seachVC animated:YES];
}
#pragma mark -
#pragma mark - **************** collectionViewDelegate

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.allGoodItems.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeGoodItemCCell * itemCellMoudle = mGetCollectionViewCell(@"homeGoodItemCCell", indexPath);
    AppBaseGoodItem * itemModel = self.allGoodItems[indexPath.row];
    itemCellMoudle.goodItem = itemModel;
    itemCellMoudle.stateLabel.text = @"专享";
    //    WeakSelf(weakSelf);
    itemCellMoudle.addCartBlock = ^{//app_shoppingCart_insert
        AddCartOrBuyView * popView = [[[NSBundle mainBundle] loadNibNamed:@"AddCartOrBuyView" owner:self options:nil] firstObject];
        popView.axcUI_Size = CGSizeMake(mScreenWidth, mScreenHeight*2/3);
        popView.goodItem = itemModel;
        popView.pushRoot = self;
        [GKCover translucentCoverFrom:mKeyWindow content:popView animated:YES];
    };
    return itemCellMoudle;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((mScreenWidth-15)/2,(mScreenWidth-15)*0.5+95);
    
}
//这个是两行cell之间的间距（上下行cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}
//两个cell之间的间距（同一行的cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 5, 5, 5);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AppBaseGoodItem * goodItem = self.allGoodItems[indexPath.row];
    MDDGoodsDetailViewController * detailVC = mGetStoryboardViewController(@"Main", @"mDDGoodsDetailViewController");
    detailVC.goodId = goodItem.gid;
    NavigationController_pushVC(detailVC);
}

#pragma mark - MoreDropDownMenuDataSource and MoreDropDownMenuDelegate
- (NSInteger)numberOfColumnsInMenu:(MoreDropDownMenu *)menu{
    return self.goodKindItems.count;
}

- (NSInteger)menu:(MoreDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column{
    ShopKindItem * item = self.goodKindItems[column];
    return item.list.count;
}

- (NSString *)menu:(MoreDropDownMenu *)menu titleForRowAtIndexPath:(MoreIndexPath *)indexPath{
    ShopKindItem * item = self.goodKindItems[indexPath.column];
    return item.list[indexPath.row][@"typeName"];
}
- (NSString *)menu:(MoreDropDownMenu *)menu titleForRowHeaderAtIndexPath:(MoreIndexPath *)indexPath
{
    ShopKindItem * item = self.goodKindItems[indexPath.column];
    return item.typeName;
}
- (NSArray *)menu:(MoreDropDownMenu *)menu arrayForRowAtIndexPath:(MoreIndexPath *)indexPath{
    ShopKindItem * item = self.goodKindItems[indexPath.column];
    return item.list;
}

- (void)menu:(MoreDropDownMenu *)menu didSelectRowAtIndexPath:(MoreIndexPath *)indexPath{
    ShopKindItem * item = self.goodKindItems[indexPath.column];
    self.selTwoKinds = item.list[indexPath.row];
    self.page = 1;
    [self loadAllGoods];
    
    if (indexPath.item >= 0) {
        NSLog(@"点击了 %ld - %ld - %ld 项目",indexPath.column,indexPath.row,indexPath.item);
    }else {
        NSLog(@"点击了 %ld - %ld 项目",indexPath.column,indexPath.row);
    }
}

- (void)didTapMenu:(MoreDropDownMenu *)menu andIndex:(NSInteger)tapIndex{
    //    if (self.moveScroll) {
    //        self.moveScroll(self.tableView);
    //    }
    ShopKindItem * item =  self.goodKindItems[tapIndex];
    self.selKindItem = item;
    self.selTwoKinds = nil;
    self.page = 1;
    [self loadAllGoods];
    //    [self.selfCollectionView.mj_header beginRefreshing];
    NSLog(@"点击了菜单");
}
#pragma mark-- loayLoad
-(MoreDropDownMenu *)menu
{
    if (!_menu) {
        CGFloat y = getRectNavAndStatusHight;
        _menu = [[MoreDropDownMenu alloc] initWithOrigin:CGPointMake(0, y) andHeight:44];
        _menu.delegate = self;
        _menu.dataSource = self;
    }
    return _menu;
}

#pragma mark -
#pragma mark - **************** 数据请求
-(void)loadGoodKinds
{
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    [PPNetworkHelper POST:app_mall_classify parameters:parameters success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            ShopKindItem * allKinds = [[ShopKindItem alloc]init];
            allKinds.cfid = @"1000";
            allKinds.typeName = @"全部";
            allKinds.list = [[NSMutableArray alloc]initWithArray:@[@{@"typeName":@"全部",@"cfId":@"999",@"parentId":@"1000"}]];
            [self.goodKindItems addObject:allKinds];
            for (NSDictionary * dataD in responseObject[@"data"]) {
                ShopKindItem * item = [[ShopKindItem alloc]init];
                item.typeName = dataD[@"typeName"];
                item.cfid = dataD[@"cfId"];
                item.parentId = dataD[@"parentId"];
                item.sortId = dataD[@"sortId"];
                NSMutableArray * listArr = [[NSMutableArray alloc]initWithArray:@[@{@"typeName":@"全部",@"cfId":@"999",@"parentId":item.cfid}]];
                for (NSDictionary * infoDic in dataD[@"list"]) {
                    [listArr addObject:infoDic];
                }
                item.list = listArr;
                [self.goodKindItems addObject:item];
            }
            [self.view addSubview:self.menu];
            [self.menu selectIndexPath:[MoreIndexPath indexPathWithCol:0 row:0]];
        }else{
            
        }
    } failure:^(NSError *error) {
        
    } showLoading:nil];
}
-(void)loadAllGoods
{
    if ([self.selfCollectionView.mj_header isRefreshing]) {
        self.page = 1;
    }
    if ([self.selfCollectionView.mj_footer isRefreshing]) {
        self.page ++;
    }
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    setDickeyobj(parameters, @"10", @"limit");
    NSString * pageStr = [NSString stringWithFormat:@"%ld",self.page];
    setDickeyobj(parameters, pageStr, @"page");
    if (self.selKindItem && self.selKindItem.cfid.integerValue != 1000) {
        
        setDickeyobj(parameters, self.selKindItem.cfid, @"classifyParentId");
    }
    if (self.selTwoKinds && self.selKindItem) {
        if ([self.selTwoKinds[@"cfId"] integerValue] != 999) {
            setDickeyobj(parameters, self.selTwoKinds[@"cfId"], @"classifyId");
        }
        
    }
    [PPNetworkHelper POST: app_mall_plantGoods parameters:parameters success:^(id responseObject) {
        [self.selfCollectionView.mj_header endRefreshing];
        [self.selfCollectionView.mj_footer endRefreshing];
        if (self.page == 1 && self.allGoodItems.count > 0) {
            [self.allGoodItems removeAllObjects];
        }
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * dataD in responseObject[@"data"][@"list"]) {
                AppBaseGoodItem * item = [[AppBaseGoodItem alloc]init];
                [item setValuesForKeysWithDictionary:dataD];
                [self.allGoodItems addObject:item];
            }
            if ([responseObject[@"data"][@"list"] count] < 10) {
                [self.selfCollectionView.mj_footer endRefreshingWithNoMoreData];
                
            }
        }else{
            
        }
        [self.selfCollectionView reloadData];
    } failure:^(NSError *error) {
        [self.selfCollectionView.mj_header endRefreshing];
        [self.selfCollectionView.mj_footer endRefreshing];
    } showLoading:nil];
}


@end
